//
//  EPCHomeTitleButton.swift
//  Weibo
//
//  Created by LioWu on 11/04/2017.
//  Copyright © 2017 MyGit. All rights reserved.
//

import UIKit

class EPCHomeTitleButton: UIButton {
    
    var hasSetted:Bool = false

    init(title: String?) {
    
        super.init(frame:CGRect.zero)
        
        if title == nil {
            setTitle("首页", for: [])
        } else {
            setTitle(title! + " ", for: [])
            
            setImage(UIImage.init(named: "navigationbar_arrow_down"), for: [])
            setImage(UIImage.init(named: "navigationbar_arrow_up"), for: .selected)
        }
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    // FIXME: - 为什么会跑两次？
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if imageView?.frame == CGRect.zero {
            return
        }
        
        // 将 label 的 x 向左移动 imageView 的宽度
        // OC 中不允许直接修改`结构体内部的值`
        // Swift 中可以直接修改
        titleLabel?.frame.origin.x = 0
        
        // 将 imageView 的 x 向右移动 label 的宽度
        imageView?.frame.origin.x = (titleLabel?.bounds.width)!
    }

}
